package com.artogrid.framework.commons.utils;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * Retrieves environment specific properties from a file located in the
 * classpath.
 * 
 */
public class Props {

	private static Logger logger = LoggerFactory.getLogger(Props.class);
	
	
	
	private static String defaultEnvProp = System.getProperty("user.dir").replace("\\", "/")+"/config"+"/dev.properties";
	private static Properties envProps;

	static {
		try {
			logger.debug("defaultEnvProp:"+defaultEnvProp);
			InputStream resourceAsStream = new FileInputStream(defaultEnvProp);
			envProps = new Properties();
			envProps.load(resourceAsStream);
			resourceAsStream.close();
		} catch (IOException e) {
			logger.error("Error: Cannot load env props file.", e);
		} catch (Exception e) {
			logger.error("Error: Cannot load env props file.", e);
		}

	}

	
	public static String getProperty(String prop, String defaultValue) {
		String propValue = null;
		if (envProps != null) {
			propValue = envProps.getProperty(prop);
		}
		if (StringUtils.isBlank(propValue)) {
			propValue = defaultValue;
		}
		return propValue;
	}

	public static String getProperty(String prop) {
		return getProperty(prop, null);
	}

	public static int getIntProperty(String prop, String defaultValue) throws NumberFormatException {
		String value = getProperty(prop, defaultValue);
		return Integer.parseInt(value);
	}
}
